/*
 * Medicine.java
 *
 * Created on August 30, 2008, 1:17 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Project.bussiness;

import Project.DBHelper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *
 * @author haolt_c0708l
 */
public class Medicine {
    
private int MedicineID;
private String MedicineName;
private int Price;
private String Component;
private String Uses;
private String Notes;
private int Insurance;

    public int getMedicineID() {
        return MedicineID;
    }

    public void setMedicineID(int MedicineID) {
        this.MedicineID = MedicineID;
    }

    public String getMedicineName() {
        return MedicineName;
    }

    public void setMedicineName(String MedicineName) {
        this.MedicineName = MedicineName;
    }

    public int getPrice() {
        return Price;
    }

    public void setPrice(int Price) {
        this.Price = Price;
    }

    public String getComponent() {
        return Component;
    }

    public void setComponent(String Component) {
        this.Component = Component;
    }

    public String getUses() {
        return Uses;
    }

    public void setUses(String Uses) {
        this.Uses = Uses;
    }

    public String getNotes() {
        return Notes;
    }

    public void setNotes(String Notes) {
        this.Notes = Notes;
    }
    
    public int getInsurance() {
        return Insurance;
    }

    public void setInsurance(int Insurance) {
        this.Insurance = Insurance;
    }
    public static Vector getAllMedicine() throws SQLException
    {
        Vector v = new Vector();
        try {
        ResultSet rs = DBHelper.executeQuery("spGetAllMedicines");
            while(rs.next()){
                Medicine m = new Medicine();
                m.setMedicineID(rs.getInt(1));
                m.setMedicineName(rs.getString(2));
                m.setPrice(rs.getInt(3));
                m.setComponent(rs.getString(4));
                m.setUses(rs.getString(5));
                m.setNotes(rs.getString(6));
                m.setInsurance(rs.getInt(7));
                v.add(m);
            }
        } catch(SQLException sqle) {
            throw sqle;
        }
        return v;        
    }   
    
    public static Vector getMedicineById(int id) throws SQLException
    {
        Vector v = new Vector();        
        try {
            // Create param list
            Vector paramList = new Vector();
            paramList.add(new Integer(id));
            ResultSet rs = DBHelper.executeQuery("spSearchMedicineById", paramList);
            
            while(rs.next()){
                Medicine m = new Medicine();
                m.setMedicineID(rs.getInt(1));
                m.setMedicineName(rs.getString(2));
                m.setPrice(rs.getInt(3));
                m.setComponent(rs.getString(4));
                m.setUses(rs.getString(5));
                m.setNotes(rs.getString(6));
                m.setInsurance(rs.getInt(7));
                v.add(m);
            }
        } catch(SQLException sqle) {
            // Handle exception            
            throw sqle;
        }   
        return v;
    }     
    
    public static Vector getMedicineByName(String name) throws SQLException
    {
        Vector v = new Vector();        
        try {
            // Create param list
            Vector paramList = new Vector();
            paramList.add(name);
            ResultSet rs = DBHelper.executeQuery("spSearchMedicineById", paramList);           
            while(rs.next()){
                Medicine m = new Medicine();
                m.setMedicineID(rs.getInt(1));
                m.setMedicineName(rs.getString(2));
                m.setPrice(rs.getInt(3));
                m.setComponent(rs.getString(4));
                m.setUses(rs.getString(5));
                m.setNotes(rs.getString(6));
                m.setInsurance(rs.getInt(7));
                v.add(m);
            }
        } catch(SQLException sqle) {
            // Handle exception            
            throw sqle;
        }   
        return v;
    }     

    public static int updateMedicine(int id, Medicine m) throws SQLException
    {
        int i;
        try{
            Vector paramList = new Vector();
            paramList.add(new Integer(id));
            paramList.add(m.MedicineName);
            paramList.add(m.Price);
            paramList.add(m.Component);
            paramList.add(m.Uses);
            paramList.add(m.Notes);
            paramList.add(m.Insurance);
            i=DBHelper.executeUpdate("spUpdateMedicine",paramList);
        } catch(SQLException e)
        {
            throw e;
        }
        return i;
    }    

    public static int insertMedicine( Medicine m) throws SQLException
    {
        int i;
        try{
            Vector paramList = new Vector();
            paramList.add(m.MedicineName);
            paramList.add(m.Price);
            paramList.add(m.Component);
            paramList.add(m.Uses);
            paramList.add(m.Notes);
            paramList.add(m.Insurance);
            i=DBHelper.executeUpdate("spInsertMedicine",paramList);
        } catch(SQLException e)
        {
            throw e;
        }
        return i;
    }     
    public static int deleteMedicine(int id) throws SQLException
    {
        int i;
        try{
            Vector paramList = new Vector();
            paramList.add(new Integer(id));
            i=DBHelper.executeUpdate("spDeleteMedicine",paramList);
        } catch(SQLException e)
        {
            throw e;
        }
        return i;
    }    


}
